<template>
	<view class="shareBonusRecord-container">
		<view class="top-box flexWrapNo">
			<view class="flexWrapNo" style="gap: 20rpx;">
				<image src="@/static/index/calendar.png" mode="widthFix" class="calendar-icon"></image>
				<picker mode="date" fields="month" :data-index='0' :value="startTime" @change="bindDateChange" :end="startLimit">
					<view class="select-input" v-if="startTime">{{startTime}}</view>
					<view class="placeClass" v-else>开始日期</view>
				</picker>
				~
				<picker mode="date" fields="month" :data-index='1' :value="date" @change="bindDateChange" :start="startTime" :end="currentDate">
					<view class="select-input" v-if="endTime">{{endTime}}</view>
					<view class="placeClass" v-else>结束日期</view>
				</picker>
			</view>
			<view class="search-btn flexWrapNo" @tap="freshList">查询</view>
		</view>
		
		<!-- 列表数据 -->
		<view class="table-list">
			<view class="table-item table-head">
				<view class="date">日期</view>
				<view class="mobile">手机号码</view>
				<view class="bonus">分红</view>
				<view class="status">状态</view>
				<view class="operate">操作</view>
			</view>
			<view class="table-item" v-for="(item,index) in list" :key="index">
				<view class="date">{{item.date}}</view>
				<view class="mobile">{{item.mobile}}</view>
				<view class="bonus">{{item.divide}}</view>
				<view class="status yellow" v-if="item.divide==0">无需发放</view>
				<view class="status" :class="{yellow:item.status==1}" v-else>{{item.status==1?'待发放':'已发放'}}</view>
				<view class="operate">
					<view v-if="item.status==2" @tap="lookAttachment(item)">查看附件</view>
				</view>
			</view>
		</view>
		
		<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
		<view class="quesheng" v-if="showEmpty">
			<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
		</view>
		
		<view class="contact-box flexWarpCenterColumn" @tap="callPhone">
			<text>遇到问题？联系我们</text>
			<text class="mobile">19013282870</text>
		</view>
	</view>
</template>

<script>
	import utils from '@/common/lib/util.js'
	import base from '@/common/base.js';
	export default{
		data(){
			const currentDate = this.getDate({
				format: true
			})
			return{
				storeId:'',
				startTime:null,
				endTime:null,
				currentDate:currentDate,
				startLimit:currentDate,
				type:null,
				list:[],
				
				page: 1,
				size: 30,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
			}
		},
		watch:{
			endTime(val){
				if(val){
					this.startLimit=val
				}else{
					this.startLimit=this.currentDate
				}
			},
		},
		onReachBottom() {
			if (this.more) {
				this.getList()
			}
		},
		onLoad(options) {
			this.type=options.type
			this.storeId=options.storeId
			this.getList()
		},
		methods:{
			// 获取日期 --当月
			getDate(type) {
				const date = new Date();
				date.setDate(date.getDate());
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				// return `${year}-${month}-${day}`
				return `${year}-${month}`
			},
			
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.startTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
			
			freshList:utils.debounce(function(){
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.list = []
				this.getList();
			},600),
			
			//  获取分红记录列表
			getList(){
				this.$request.post(this.$api.bonusList, {
					storeId:this.storeId,
					mobile:uni.getStorageSync('mobile'),
					start:this.startTime,
					end:this.endTime,
					status:this.type,
					pageNum:this.page++,  //当前页数
					pageSize:this.size,  //当前条数
				}, {
					isJava: true
				}).then(res => {
					if(res.code==200){
						let {
							data = {}
						} = res;
						let newArray = data.list
						
						if (newArray.length == 0 && this.page == 2) {
							this.showEmpty = true
							this.showLoadStatus = false
							this.more = false // 不能再次请求了
							return false;
						} else {
							this.showLoadStatus = true
							this.more = true
							this.showEmpty = false
							this.status = 'loading'
						}
						if (newArray.length < this.size) { // 说明没有更多数据
							this.status = 'noMore'
							this.more = false
							this.showEmpty = false
						}
						this.list = this.list.concat(newArray);
					}
				})
			},
			
			// 查看附件
			lookAttachment(item){
				let imageList
				if(item.attachment){
					imageList=item.attachment.split(',')
					this.previewImage(0,imageList)
				}else{
					this.$tools.showToast('无附件可查看')
				}
			},
			
			
			
			callPhone(){
				uni.makePhoneCall({
					phoneNumber: '19013282870', // 电话号码
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.shareBonusRecord-container{
		padding: 0 28rpx;
		padding-bottom: 80rpx;
	}
	.top-box{
		padding: 30rpx 0;
		gap: 20rpx;
		background: #040336;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: sticky;
		top: 0;
		.calendar-icon{
			width: 36rpx;
		}
		picker{
			font-size: 28rpx;
		}
		.search-btn{
			padding: 0 30rpx;
			height: 60rpx;
			border-radius: 8rpx;
			color: #fff;
			font-size: 28rpx;
			background: rgb(31, 124, 247);
		}
	}
	
	.table-list{
		margin-top: 10rpx;
		margin-bottom: 90rpx;
		background: #1D1C5C;
		border-radius: 8rpx;
		.table-item{
			display: flex;
			color: #fff;
			font-size: 24rpx;
			text-align: center;
			border-bottom:1rpx solid  rgba(255,255,255,0.3);
			&:last-child{
				border-bottom: unset;
			}
			>view{
				padding: 16rpx 10rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.date{
				width: 170rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.mobile{
				width: 170rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.bonus{
				flex: 1;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.status{
				width: 100rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.yellow{
				color: #FFE577;
			}
			.operate{
				width: 130rpx;
				>view{
					color: #26BCFD;
				}
			}
		}
		.table-head{
			>view{
				font-weight: bold;
				font-size: 26rpx !important;
				padding: 14rpx 10rpx;
			}
		}
	}
	
	.quesheng{
		margin-top: 30rpx;
		display: flex;
		justify-content: center;
		image{
			width: 476upx;
			height: 280upx;
		}
	}
	
	.contact-box{
		width: 100%;
		font-size: 28rpx;
		color: rgba(255,255,255,0.6);
		background: #040336;
		padding-top: 10rpx;
		padding-bottom: 40rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		.mobile{
			color: rgba(255,255,255,0.8);
			font-weight: bold;
		}
	}
</style>